import * as VueRouter from 'vue-router'
import MainLayout from '@/components/MainLayout.vue'
import {ElMessage as Message} from 'element-plus'

const router = VueRouter.createRouter({
	history: VueRouter.createWebHistory(), routes: [{
		path: '/', component: MainLayout, meta: {showSidebar: false}, children: [{
			path: '/login',
			name: 'LoginVue',
			meta: {showSidebar: false, pageText: '登录'},
			component: () => import('../views/Login.vue'),
		}, {
			path: '/ConferenceRoomManagement',
			name: 'ConferenceRoomManagement',
			meta: {showSidebar: true, pageText: '会议室管理'},
			component: () => import('../views/ConferenceRoomManagement.vue'),
		}, {
			path: '/ConferenceRoom',
			name: 'ConferenceRoom',
			meta: {showSidebar: true, pageText: '会议室'},
			component: () => import('../views/ConferenceRoomManagement.vue'),
		}, {
			path: '/ConferenceList',
			name: 'ConferenceList',
			meta: {showSidebar: true, pageText: '会议列表'},
			component: () => import('../views/ConferenceList.vue'),
		}, {
			path: 'ConferenceReservations',
			name: 'ConferenceReservations',
			meta: {showSidebar: true, pageText: '预约会议'},
			component: () => import('../views/ConferenceReservations.vue'),
		}, {
			path: 'MeetingUsage',
			name: 'MeetingUsage',
			meta: {showSidebar: true, pageText: '会议室使用情况'},
			component: () => import('@/views/ConferenceFeedBack.vue'),
			props: true,
		}, {
			path: 'UserManagement',
			name: 'UserManagement',
			meta: {showSidebar: true, pageText: '用户管理'},
			component: () => import('../views/UserManagement.vue'),
		}, {
			path: 'CampusManagement',
			name: 'CampusManagement',
			meta: {showSidebar: true, pageText: '校区管理'},
			component: () => import('../views/CampusManagement.vue'),
		}, {
			path: 'Dashboard',
			name: 'Dashboard',
			meta: {showSidebar: true, pageText: '首页'},
			component: () => import('../views/DashBoard.vue'),
		}, {
			path: 'AnnouncementManagement',
			name: 'AnnouncementManagement',
			meta: {showSidebar: true, pageText: '公告管理'},
			component: () => import('../views/AnnouncementManagement.vue'),
		}, {
			path: 'ConferenceSummary',
			name: 'ConferenceSummary',
			meta: {showSidebar: true, pageText: '会议纪要'},
			component: () => import('@/views/ConferenceSummary.vue'),
		},{
			path: 'DepartmentManagement',
			name: 'DepartmentManagement',
			meta: {showSidebar: true,pageText: '部门管理'},
			component: () => import('../views/DepartmentManagement.vue'),
		}
		],
	},],
})

router.beforeEach((to, from, next) => {
	const token = localStorage.getItem('WFITToken');
	if (to.path === '/login') {
		next(); // 如果是登录页面，直接放行
		return
	}
	if (!token) {
		Message.error('请先登录')
		next('/login'); // 如果没有token，跳转到'/login'
		return;
	}
	// 如果存在token
	// 检查路径是否为''
	if (to.path === '/') {
		// 从本地存储获取token和用户类型
		const role = localStorage.getItem('WFITRole');
		// 根据用户类型进行跳转
		if (role === '1') {
			next('/dashboard'); // 用户类型为1，跳转到'/dashboard'
		} else if (role === '0') {
			next('/ConferenceReservations'); // 用户类型为2，跳转到'/ConferenceReservations'
		} else {
			next('/login'); // 如果用户类型不是1或2，或者没有用户类型，跳转到'/login'
		}
	} else {
		next(); // 如果不是根路径，继续正常的路由
	}
});

export default router


